package com.meihua.baoleyi.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.view.CatLoadingView;

/**
 * Created by liming on 2017/10/24.
 * 百科详情 feiqi
 */

public class BaikeDetailsActivity  extends BaseActivity {

    private CatLoadingView date;
    private WebView webview;
    private WebViewClient mWebViewClient;
    private boolean showdelte = true;
    private int id; //百科详情id
    private ImageView back;

    public static Intent createIntent(Context mContext, int id) {
        Intent intent = new Intent(mContext, BaikeDetailsActivity.class);
        intent.putExtra("id", id);
        return  intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_baike_details);
        id = getIntent().getIntExtra("id", 0);
        initView();
        if (date == null) {
            date = new CatLoadingView();
        }
    }

    /**
     * 初始化WebView
     */
    private void initView() {
        webview = (WebView) findViewById(R.id.webview);
        back = findViewById(R.id.iv_back);
        initWebViewClient();
        showWebView();
        //返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 显示WebView
     */
    private void showWebView() {
        WebSettings webSetting = webview.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDefaultTextEncodingName("utf-8");
        String token=myApp.getToken().toString();
        String userid = myApp.getUser_id();
        webview.loadUrl(APIServer.KNOWDETAIL+id+"/"+token);
        webview.setWebViewClient(mWebViewClient); //设置WebViewClient
    }

    /**
     * 设置WebViewClient
     */
    private void initWebViewClient() {
        mWebViewClient = new WebViewClient(){
            //页面开始加载时回调
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (date.isAdded()){
                    date.dismiss();
                }else if (showdelte){
//                    date.show(getSupportFragmentManager(), "");
                    showdelte = false;
                }
            }
            //页面加载完成时回调
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (date !=null){
                    date.dismiss();
                }
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        };
    }

}
